package com.example.administrator.newsmvp.news.widget;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.newsmvp.R;
import com.example.administrator.newsmvp.beans.NewsBean;
import com.example.administrator.newsmvp.news.adapter.NewsAdapter;
import com.example.administrator.newsmvp.news.presenter.NewsPresenter;
import com.example.administrator.newsmvp.news.view.NewsView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class NewsListFragment extends Fragment implements NewsView {

    @InjectView(R.id.recycle_view)
    RecyclerView mRecycleView;
    @InjectView(R.id.swipe_refresh_widget)
    SwipeRefreshLayout mSwipeRefreshWidget;
    private String mType;
    private NewsPresenter mPresenter;
    private Activity mActivity;
    private NewsAdapter mAdapter;

    public static NewsListFragment newInstance(String type) {
        Bundle args = new Bundle();
        args.putString("type", type);
        NewsListFragment fragment = new NewsListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mType = getArguments().getString("type");
        mActivity = getActivity();
        mPresenter = new NewsPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_newslist, container, false);
        ButterKnife.inject(this, view);
        mRecycleView.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new NewsAdapter(mActivity);
        mRecycleView.setAdapter(mAdapter);

        mPresenter.loadData(mType);
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void failure() {
        Toast.makeText(getActivity(), "加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loadData(List<NewsBean> datas) {
        mAdapter.setData(datas);

    }
}
